package com.logicwin.RobotTravel;

import com.logicwin.RobotTravel.Screens.Screen;
import com.logicwin.RobotTravel.Screens.Screen.SCREEN_TYPE;

import com.logicwin.RobotTravel.Services.IScreenService;
import com.logicwin.RobotTravel.Services.Impl.ServiceManager;

import com.logicwin.RobotTravel.R;
import com.logicwin.RobotTravel.Screens.ScreenHome;

import android.app.ActivityGroup;
//import android.app.ActivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

import android.widget.Toast;
import android.content.*;

public class Main extends ActivityGroup {

	public static final int ACTION_NONE = 0;
	public static final int ACTION_SHOW_AVSCREEN = 1;
	public static final int ACTION_RESTORE_LAST_STATE = 2;
	public static final int ACTION_SHOW_HISTORY = 3;
	public static final int ACTION_SHOW_MSRP_INC_SCREEN = 4;
	public static final int ACTION_SHOW_CONTSHARE_SCREEN = 5;

	public static int screenWidth;
	public static int screenHeight;
	private static Main mainActivity;
	private final IScreenService screenService;
	private final Handler handler;

	private static String TAG = Main.class.getCanonicalName();

	private static Context mContext;

	public static Main getMainActivity() {
		return mainActivity;
	}

	public Main() {
		super();
		mainActivity = this;
		ServiceManager.setMainActivity(this);
		this.screenService = ServiceManager.getScreenService();
		this.handler = new Handler();
		mContext = this;

	}

	public static void makeToast(String text) {
		Toast.makeText(mContext, text, 300);
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// remove title bar
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);		
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);		
		setContentView(R.layout.main);
		
		Display display = getWindowManager().getDefaultDisplay();
		screenWidth = display.getWidth();
		screenHeight = display.getHeight();			
		// starts our services (will do nothing if already started)
		if (!ServiceManager.start()) {
			Log.e(Main.TAG, "Failed to start services");
			return; // Should exit
		}
		

		
		this.screenService.show(ScreenHome.class);

	}

	public void exit() {
		/*
		 * ActivityManager am =
		 * (ActivityManager)getSystemService(ACTIVITY_SERVICE);
		 * am.killBackgroundProcesses("com.logicwin.RobotTravel");
		 */
		ServiceManager.stop();
		this.handler.post(new Runnable() {
			public void run() {
				Main.this.finish();
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Screen current = this.screenService.getCurrentScreen();
		if (current != null) {
			if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0
					&& current.getType() != SCREEN_TYPE.HOME_T) {
				this.screenService.back();
				return true;
			} else if (keyCode == KeyEvent.KEYCODE_MENU
					&& event.getRepeatCount() == 0) {
				if (!current.haveMenu()) {
					this.screenService.show(ScreenHome.class);
					return true;
				} else {
					return current.onKeyDown(keyCode, event);
				}
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	public void setProgressInfo(String value) {
	}
}
